package io.sage.modules.basics.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.sage.common.utils.R;
import io.sage.common.utils.StringUtils;
import io.sage.modules.basics.entity.BWarehouse;
import io.sage.modules.basics.entity.BWarehouseType;
import io.sage.modules.basics.service.BWarehouseService;
import io.sage.modules.basics.service.BWarehouseTypeService;
import io.sage.modules.sys.entity.SysUserEntity;
import io.sage.modules.sys.service.SysDeptService;
import io.sage.modules.sys.shiro.ShiroUtils;
import io.swagger.models.auth.In;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * @author xujh
 * @email jhang_mail@163.com
 * @date 2018-07-09 13:48:15
 */
@RestController
@RequestMapping("basics/bwarehousetype")
public class BWarehouseTypeController {

	@Autowired
	private BWarehouseTypeService warehouseTypeService;

	@Autowired
	private SysDeptService sysDeptService;
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("basics:bwarehousetype:list")
	public R list(@RequestParam Map<String, Object> params) {

		Integer pageSize = 0;
		if (params != null && params.get("limit") != null) {
			pageSize = java.lang.Integer.parseInt(params.get("limit").toString());
		}
		Integer pageNum = 10;
		if (params != null && params.get("page") != null) {
			pageNum = java.lang.Integer.parseInt(params.get("page").toString());
		}

		BWarehouseType bWarehouseType=new BWarehouseType();
		if(params.get("antennaTypeName")!=null){

			if(!"".equals(params.get("antennaTypeName").toString())){
				bWarehouseType.setAntennaTypeName(params.get("antennaTypeName").toString());
			}
		}
		PageHelper.startPage(pageNum, pageSize);// 设置分页的起始码以及页面大小
		//paramMap.put("orderby","w.update_time desc,w.create_time desc");
		List<BWarehouseType> selectWarehouseList = warehouseTypeService.queryAll(bWarehouseType);

		PageInfo<BWarehouseType> pageInfo = new PageInfo<BWarehouseType>(selectWarehouseList);// 传入list就可以了

		return R.ok().put("warehouseTypeList", pageInfo);
	}


	/**
	 * 信息
	 */
	@RequestMapping("/info/{id}")
	@RequiresPermissions("basics:bwarehousetype:info")
	public R info(@PathVariable("id") Integer id){

		BWarehouseType warehouseType = warehouseTypeService.queryById(id);
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("warehouseType", warehouseType);
		return R.ok().put("map", paramMap);
	}
	
	/**
	 * 保存
	*/
	@RequestMapping("/save")
	@RequiresPermissions("basics:bwarehousetype:save")
	public R save(@RequestBody JSONObject jsonObject){
		BWarehouseType warehouse = jsonObject.getObject("bWarehouseType", BWarehouseType.class);

		SysUserEntity user = ShiroUtils.getUserEntity();
		warehouse.setCreateUserId(user.getUserId().intValue());
		warehouse = warehouseTypeService.insert(warehouse);

		if(warehouse.getId()!=null){
			return R.ok();
		}

		return R.error();
	}
	
	/**
	 * 修改
	*/
	@RequestMapping("/update")
	@RequiresPermissions("basics:bwarehousetype:update")
	public R update(@RequestBody JSONObject jsonObject){
		BWarehouseType warehouse = jsonObject.getObject("bWarehouseType", BWarehouseType.class);
		
		if(warehouse.getId()==null){
			return R.error("需要修改的id，不能为空");
		}
		int update = warehouseTypeService.update(warehouse);//全部更新

		if(update>0){
			return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 批量删除.
	 */
	@RequestMapping("/delete")
	@RequiresPermissions("basics:bwarehousetype:delete")
	public R delete(@RequestBody Integer[] ids){
		List<Integer> idslist=new ArrayList<Integer>(Arrays.asList(ids));
		Map<String,Object> map=new HashMap<>();
		List<BWarehouseType>list=warehouseTypeService.selectIsUse(ids);
		StringBuffer stringBuffer=new StringBuffer();
		for (BWarehouseType bwarehouse:list) {
			stringBuffer.append(bwarehouse.getAntennaTypeName());
			idslist.remove(bwarehouse.getId());
			stringBuffer.append(",");
		}

		ids=idslist.toArray(new Integer[idslist.size()]);
		int delete=0;
		if(ids.length>0){
			delete= warehouseTypeService.deleteBatchIds(ids);
		}
		if(list.size()>0){
			String msg=stringBuffer.toString().substring(0,stringBuffer.toString().length() - 1);
			map.put("msg",msg+"已被使用，无法删除");
		}else{
			map.put("msg","删除成功");
		}
		if(delete==ids.length){
			return R.ok().put("data",map);
		}

		return R.error();
	}

}
